#include<iostream>
#include<memory.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
int main(int argc,char* argv[])
{
    if(argc!=3)
    {
        std::cout<<"UDPclient"<<" serveip serveport";
        exit(0);
    }
    std::string serve_ip=argv[1];
    uint16_t serve_port=std::stoi(argv[2]);
   
    int sockid=socket(AF_INET,SOCK_DGRAM,0);
    if(sockid<0)
    {
        std::cout<<"socket error"<<std::endl;
    }

    struct sockaddr_in server;
    memset(&server,0,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(serve_port);
    server.sin_addr.s_addr=inet_addr(serve_ip.c_str());

    while(true)
    {
        std::string input;
        std::cout<<"please entre"<<": ";
        std::getline(std::cin,input);

        int n=sendto(sockid,input.c_str(),input.size(),0,(struct sockaddr*)&server,sizeof(server));
        (void)n;
        char buffer[1024];
        struct sockaddr_in peer;
        socklen_t len=sizeof(peer); 
        int m=recvfrom(sockid,buffer,sizeof(buffer)-1,0,(struct sockaddr*)&peer,&len);
        if(m>0)
        {
            buffer[m]=0;
            std::cout<<buffer<<std::endl;
        }

    }

    return 0;
}